package com.humanres.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AddTripFlowReq;
import com.ciko.code.http.Req.GetNextPersonReq;
import com.ciko.code.type.EmployeeEnum;
import com.ciko.code.utils.LogUtils;
import com.ciko.code.utils.Util;
import com.mywork.R;
import com.squareup.picasso.Picasso;
import com.ui.dialog.DataTimePicker;

import java.util.Calendar;
import java.util.Locale;


public class ResOutActivity
        extends
        BaseActicity
{
	
	private RelativeLayout rectchooseType;
	private RelativeLayout rectchooseTimeStart;
	private RelativeLayout rectchooseTimeEnd;
						   
	private TextView	   tv_startTimeLeave;
	private TextView	   tv_endTimeLeave;
	private TextView	   tv_dayCountLeave;
	private TextView	   tv_Job;
	private TextView	   tv_Name;
	private TextView	   tv_Retroactive;
	private ImageView	   ivHanderIcon;
						   
	private EditText	   etAdressOut;
	private EditText	   etresonLeave;
						   
	private Button		   btnsubmitLeave;
						   
	//    private DatePickerDialog startTimeDialog;
	//
	//    private long startTime = 0;
	//    private long endTime = 0;
	//
	//    private DatePickerDialog endTimeDialog;
	
	private DataTimePicker startTimeDialog;
						   
	private long		   startTime = 0;
	private long		   endTime	 = 0;
									 
	private DataTimePicker endTimeDialog;
						   
	private int			   dayCount	 = 0;
									 
	private String		   adress;
	private String		   reson	 = "";
	private User		   nextuser	 = null;
									 
	@Override
	protected void initTopBar() {
		setTitle("出差申请");
		enableNavLeftImg();
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_res_out);
	}
	
	@Override
	protected void initView() {
		
		btnsubmitLeave = (Button) findViewById(R.id.btn_submitLeave);
		tv_startTimeLeave = (TextView) findViewById(R.id.tv_startTimeLeave);
		tv_endTimeLeave = (TextView) findViewById(R.id.tv_endTimeLeave);
		tv_dayCountLeave = (TextView) findViewById(R.id.tv_dayCountLeave);
		etresonLeave = (EditText) findViewById(R.id.et_resonLeave);
		etAdressOut = (EditText) findViewById(R.id.etAdressOut);
		rectchooseTimeEnd = (RelativeLayout) findViewById(R.id.rect_chooseTimeEnd);
		rectchooseTimeStart = (RelativeLayout) findViewById(R.id.rect_chooseTimeStart);
		rectchooseType = (RelativeLayout) findViewById(R.id.rect_chooseType);
		ivHanderIcon = (ImageView) findViewById(R.id.ivHanderIcon);
		tv_Job = (TextView) findViewById(R.id.tvJob);
		tv_Name = (TextView) findViewById(R.id.tvName);
		//        tv_Retroactive = (TextView) findViewById(R.id.tv_Retroactive);
	}
	
	@Override
	protected void initAdapter() {
	
	}
	
	@Override
	protected void initDates() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		startTimeDialog = new DataTimePicker(ResOutActivity.this, listener);
		endTimeDialog = new DataTimePicker(ResOutActivity.this, endlistener);
		//        startTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
		//            @Override
		//            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		//                tv_startTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
		//                tv_startTimeLeave.setTextColor(Color.BLACK);
		//                startTime = view.getCalendarView().getDate();
		//                long nowTime = System.currentTimeMillis();
		////                if (startTime>nowTime) {
		////                    tv_Retroactive.setText("否");
		////                }
		////                else {
		////                    tv_Retroactive.setText("是");
		////                }
		//                endTimeDialog.getDatePicker().setMinDate(startTime);
		//
		//                if (endTime != 0 && startTime != 0) {
		//
		//                    dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000)) + 1;
		//
		//                    tv_dayCountLeave.setText(String.valueOf(dayCount));
		//                }
		//
		//            }
		//        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		//
		//        endTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
		//            @Override
		//            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		//                tv_endTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
		//                tv_endTimeLeave.setTextColor(Color.BLACK);
		//                endTime = view.getCalendarView().getDate();
		//
		//                if (endTime != 0 && startTime != 0) {
		//                    dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000)) + 1;
		//                    tv_dayCountLeave.setText(String.valueOf(dayCount));
		//                }
		//            }
		//        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		
		//获取审批人
		doRequestHttp(new GetNextPersonReq());
	}
	
	@Override
	protected void initListener() {
		rectchooseTimeStart.setOnClickListener(this);
		rectchooseTimeEnd.setOnClickListener(this);
		
		btnsubmitLeave.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == btnsubmitLeave)
		{
			if (check())
			{
				progressDialog.show();
				doRequestHttp(new AddTripFlowReq(adress, startTime, endTime, dayCount, reson, "", nextuser.getId(), nextuser.getStaffName()));
			}
		}
		else if (v == rectchooseTimeEnd)
		{
			endTimeDialog.show();
		}
		else if (v == rectchooseTimeStart)
		{
			startTimeDialog.show();
		}
		else if (v == rectchooseType)
		{
		
		}
		//        switch (v.getId()) {
		//            case R.id.btn_submitLeave:
		//
		//                if (check()) {
		//                    doRequestHttp(new AddTripFlowReq(adress, startTime, endTime, dayCount, reson, "", WorkUIKit.getUser().getId(), App.getUser().getStaffName()));
		//                }
		//
		//                break;
		//            case R.id.rect_chooseTimeEnd:
		//                endTimeDialog.show();
		//                break;
		//            case R.id.rect_chooseTimeStart:
		//                startTimeDialog.show();
		//                break;
		//            case R.id.rect_chooseType:
		//
		//                break;
		//        }
	}
	
	private boolean check() {
		
		adress = etAdressOut.getText().toString().trim();
		reson = etresonLeave.getText().toString().trim();
		
		if (TextUtils.isEmpty(adress))
		{
			showToast("请输入出差地址");
			return false;
		}
		else if (startTime == 0)
		{
			showToast("请选择开始时间");
			return false;
		}
		else if (endTime == 0)
		{
			showToast("请选择结束时间");
			return false;
		}
		else if (dayCount <= 0)
		{
			showToast("结束时间必须大于开始时间");
			return false;
		}
		else if (TextUtils.isEmpty(reson))
		{
			showToast("请填写出差事由");
			return false;
		}
		else if (reson.length() > 50)
		{
			showToast("字数不能超过50字");
			return false;
		}
		else if (null == nextuser)
		{
			showToast("请选择审批人");
			return false;
		}
		
		return true;
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		
		switch (tag) {
			case "AddTripFlowReq":
				progressDialog.dismiss();
				showToast("提交成功");
				Intent intent = new Intent();
				setResult(1001, intent);
				finish();
				break;
			case "GetNextPersonReq":
				nextuser = (User) response.getReturnObject();
				LogUtils.d("GetNextPersonReq", nextuser.getStaffName());
				if (null != nextuser)
				{
					tv_Job.setText(EmployeeEnum.typeOfValue(nextuser.getUserPositionMark()));
					tv_Name.setText(nextuser.getStaffName());
					if (!TextUtils.isEmpty(nextuser.getUserHead()))
					                                                Picasso.with(this).load(nextuser.getUserHead()).into(ivHanderIcon);
					if (nextuser.getUserPositionMark() == 9999) {
						Util.showToast(this, R.string.params_less_tip);
					}
				}
				break;
		}
	}
	
	DataTimePicker.OnDateTimeSetListener listener	 = new DataTimePicker.OnDateTimeSetListener()
	                                                 {
		                                                 @Override
		                                                 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth, int hour, int mintue) {
			                                                 String curDate = "" + year + "-" + getTimeString(monthOfYear + 1) + "-" + getTimeString(dayOfMonth) + " " + getTimeString(hour) + ":" + getTimeString(mintue);
			                                                 tv_startTimeLeave.setText(curDate);
			                                                 tv_startTimeLeave.setTextColor(Color.BLACK);
			                                                 startTime = Util.getStringtoDate(curDate).getTime();
															 
			                                                 dipatchTime(startTime, endTime);
		                                                 }
	                                                 };
													 
	DataTimePicker.OnDateTimeSetListener endlistener = new DataTimePicker.OnDateTimeSetListener()
	                                                 {
		                                                 @Override
		                                                 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth, int hour, int mintue) {
			                                                 String curDate = "" + year + "-" + getTimeString(monthOfYear + 1) + "-" + getTimeString(dayOfMonth) + " " + getTimeString(hour) + ":" + getTimeString(mintue);
			                                                 tv_endTimeLeave.setText(curDate);
			                                                 tv_endTimeLeave.setTextColor(Color.BLACK);
			                                                 endTime = Util.getStringtoDate(curDate).getTime();
			                                                 dipatchTime(startTime, endTime);
		                                                 }
	                                                 };
													 
	private String getTimeString(int time) {
		return (time < 10 ? "0" : "") + time;
	}
	
	private void dipatchTime(long startTime, long endTime) {
		if (endTime != 0 && startTime != 0)
		{
			long mss = endTime - startTime;
			int hour = (int) (mss / (60 * 60 * 1000));
			int minutes = (int) (mss % (60 * 60 * 1000));
			
			dayCount = (hour + (minutes == 0 ? 0 : 1));
			
			System.out.println("hour:" + dayCount);
			tv_dayCountLeave.setText(Util.getDayAndHour(dayCount));
		}
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		switch (tag) {
			case "AddTripFlowReq":
                progressDialog.dismiss();
				Util.showToast(this, "提交失败，请稍后再试");
				break;
		}
	}
	
	/**
	 * @param mss
	 *            要转换的毫秒数
	 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
	 * @author fy.zhang
	 */
	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
	}
	
}
